﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StoreTransport.DAO;
using StoreTransport.Resources;
using StoreTransport.Business.Helper;

namespace StoreTransport.Presentation.Forms {
    public partial class MarketKindListForm : Form {
        int id;
        public MarketKindListForm() {
            InitializeComponent();
        }

        private void MarketKindListForm_Load(object sender, EventArgs e) {
            _tbxMarketKindName.Select();
            LoadMarketKindName();
        }

        private void LoadMarketKindName() {
            _grdMarketKinds.AutoGenerateColumns = false;
            var marketKindList = CommonHelper.GetContext().MarketKinds.
                Cast<MarketKind>().OrderBy(c => c.MarketKindName);
            _grdMarketKinds.DataSource = marketKindList;
        }

        private void _btxCreate_Click(object sender, EventArgs e) {
            _tbxMarketKindName.Select();
            _tbxMarketKindName.Text = string.Empty;
            _tbxPercent.Text = string.Empty;
            _lblID.Text = StoreTransportResource.Form_NewID;
            id = 0;
        }

        private void _btnDelete_Click(object sender, EventArgs e) {
            if (_grdMarketKinds.SelectedRows.Count > 0) {
                DialogResult result = MessageBox.Show(StoreTransportResource.Message_ConfirmDelete,
                    StoreTransportResource.MessageBox_ConfirmDelete, MessageBoxButtons.YesNo);
                if (result == System.Windows.Forms.DialogResult.Yes) {
                    try {
                        StoreTransportDataContext context = CommonHelper.GetContext();
                        foreach (DataGridViewRow row in _grdMarketKinds.SelectedRows) {
                            id = int.Parse(row.Cells[0].Value.ToString());
                            MarketKind selectedItem = context.MarketKinds.Where(c => c.ID.Equals(id)).First();
                            context.MarketKinds.DeleteOnSubmit(selectedItem);
                            context.SubmitChanges();
                        }
                        LoadMarketKindName();
                        id = 0;
                    } catch (Exception ex) {
                        MessageBox.Show(StoreTransportResource.ErrorMessage_CanNotDeleteItem);
                    }
                }
            } else {
                MessageBox.Show(StoreTransportResource.NoItemSelected);
            }
        }

        private void _btxEdit_Click(object sender, EventArgs e) {
            if (_grdMarketKinds.SelectedRows.Count > 0) {
                id = int.Parse(_grdMarketKinds.SelectedRows[0].Cells[0].Value.ToString());
                EditItem();
            }
        }

        private void EditItem() {
            MarketKind selectedItem = CommonHelper.GetContext().MarketKinds.Where(c => c.ID.Equals(id)).First();
            _tbxMarketKindName.Text = selectedItem.MarketKindName;
            _tbxPercent.Text = selectedItem.Percent.ToString();
            _lblID.Text = id.ToString();
        }

        private void _btxSave_Click(object sender, EventArgs e) {
            StoreTransportDataContext context = CommonHelper.GetContext();
            string errorMessage = CommonHelper.CheckRequiredField(_tbxMarketKindName.Text, _lblMarketKindName.Text);
            errorMessage += CommonHelper.CheckNumberValue(_tbxPercent.Text, _lblPercent.Text);
            if (!string.IsNullOrEmpty(errorMessage)) {
                MessageBox.Show(errorMessage);
                return;
            }
            MarketKind marketKind = null;
            if (id == 0) {
                if (context.MarketKinds.FirstOrDefault(c => c.MarketKindName.Equals(_tbxMarketKindName.Text)) != null) {
                    MessageBox.Show(string.Format(StoreTransportResource.Error_Dupplicate_MarketKind, _tbxMarketKindName.Text));
                    return;
                }
                marketKind = new MarketKind();
                context.MarketKinds.InsertOnSubmit(marketKind);
            } else {
                var item = context.MarketKinds.FirstOrDefault(c => c.MarketKindName.Equals(_tbxMarketKindName.Text));
                if (item != null && item.ID != id) {
                    MessageBox.Show(string.Format(StoreTransportResource.Error_Dupplicate_MarketKind, _tbxMarketKindName.Text));
                    return;
                }
                marketKind = context.MarketKinds.Where(c => c.ID.Equals(id)).First();
            }
            marketKind.MarketKindName = _tbxMarketKindName.Text;
            marketKind.Percent = int.Parse(_tbxPercent.Text);
            context.SubmitChanges();
            LoadMarketKindName();
        }

        private void _grdMarketKinds_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
            id = int.Parse(_grdMarketKinds.Rows[e.RowIndex].Cells[0].Value.ToString());
            EditItem();
        }
    }
}
